
/*
 * Copyright (C) 2008-2009 Archie L. Cobbs. All rights reserved.
 *
 * $Id: SchemaModelFactoryBean.java 320 2010-02-12 18:43:29Z archie.cobbs $
 */

package org.dellroad.sidekar.spring;

import java.util.List;

import org.dellroad.sidekar.schema.model.SchemaModel;
import org.springframework.beans.factory.config.AbstractFactoryBean;

/**
 * Spring factory bean for generating a {@link SchemaModel} by scanning the classpath
 * for {@link org.dellroad.sidekar.annotation.SidekarEntity @SidekarEntity}-annotated user model classes.
 *
 * <p>
 * Instances are most easily created using the <code>&lt;sidekar:schema base-package="..."/&gt;</code> XML tag,
 * </p>
 */
public class SchemaModelFactoryBean extends AbstractFactoryBean<SchemaModel> {

    private List<String> basePackageNames;

    /**
     * Configure the list of Java package names to search under.
     *
     * <p>
     * This property is required.
     * </p>
     */
    public void setBasePackageNames(List<String> basePackageNames) {
        this.basePackageNames = basePackageNames;
    }

    @Override
    public Class<SchemaModel> getObjectType() {
        return SchemaModel.class;
    }

    @Override
    protected SchemaModel createInstance() {
        if (this.basePackageNames == null)
            throw new IllegalArgumentException("'basePackageNames' is required");
        ClassPathEntityScanner scanner = new ClassPathEntityScanner(
          this.basePackageNames.toArray(new String[this.basePackageNames.size()]));
        return new SchemaModel(scanner.scan());
    }
}

